package C054;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

/**
 * ClassName: C03
 * Package: C054
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/3/30 12:42
 * @Version 1.0
 * @Since 1.0
 */
public class C03 {

    public static int MAXN = 100005;

    public static int[][] arr = new int[MAXN][2];

    // 数组的长度n，花盆的宽度d
    public static int n, d;

    public static int[] maxDeque = new int[MAXN];

    public static int[] minDeque = new int[MAXN];

    public static int maxh, maxt, minh, mint;

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        d = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            arr[i][0] = scanner.nextInt();
            arr[i][1] = scanner.nextInt();
        }
        int ans = compute();
        System.out.print(ans == Integer.MAX_VALUE ? -1 : ans);
    }


    private static int compute() {
        maxt = maxh = minh = mint = 0;
        int res = Integer.MAX_VALUE;
        // 排序
        Arrays.sort(arr, 0, n, (int[] a, int[] b) -> {
            return a[0] - b[0];
        });
        for (int l = 0, r = 0; l < n; l++) {
            while(r < n && !ok()) {
                push(r++);
            }
            if(ok()) {
                res = Math.min(res, arr[r - 1][0] - arr[l][0]);
            }
            // 弹出元素
            pop(l);
        }
        return res;
    }

    private static boolean ok() {
        int min = minh < mint ? arr[minDeque[minh]][1] : 0;
        int max = maxh < maxt ? arr[maxDeque[maxh]][1] : 0;
        return max - min >= d;
    }

    private static void push(int r) {
        while (maxh < maxt && arr[r][1] >= arr[maxDeque[maxt - 1]][1]) {
            maxt--;
        }
        maxDeque[maxt++] = r;
        while (minh < mint && arr[r][1] <= arr[minDeque[mint - 1]][1]) {
            mint--;
        }
        minDeque[mint++] = r;
    }

    private static void pop(int l) {
        if(maxh < maxt && maxDeque[maxh] == l) {
            maxh ++;
        }
        if(minh < mint && minDeque[minh] == l) {
            minh ++;
        }
    }


}
